package com.gin.kotlin.no_gay_alliance.utils

import android.content.Context
import android.util.TypedValue
import android.widget.TextView
import androidx.annotation.AttrRes
import com.gin.kotlin.no_gay_alliance.R
import java.time.ZonedDateTime
import java.time.format.DateTimeFormatter
import java.time.temporal.ChronoUnit

class TimeUtils {
    companion object {
        /**
         * 年到日
         */
        val DATE_FORMATTER_YEAR_DAY: DateTimeFormatter = DateTimeFormatter.ofPattern("yy-MM-dd")

        /**
         * 时分秒
         */
        val TIME_ONLY_FORMATTER: DateTimeFormatter = DateTimeFormatter.ofPattern("HH:mm:ss")

        /**
         * 月到秒
         */
        val DATE_TIME_FORMATTER_MONTH_SECOND: DateTimeFormatter = DateTimeFormatter.ofPattern("MM-dd HH:mm:ss")

        /**
         * 月到分
         */
        val DATE_TIME_FORMATTER_MONTH_MINUTE: DateTimeFormatter = DateTimeFormatter.ofPattern("MM-dd HH:mm")

        /**
         * 年月日时分秒
         */
        val FULL_DATE_TIME_FORMATTER: DateTimeFormatter = DateTimeFormatter.ofPattern("yy-MM-dd HH:mm:ss")

        /**
         *年月日时分
         */
        val SIMPLE_DATE_TIME_FORMATTER: DateTimeFormatter = DateTimeFormatter.ofPattern("yy-MM-dd HH:mm")

        /**
         * 向TextView中写入时间日期
         */
        fun zdtIntoTextView(context: Context, target: ZonedDateTime, textView: TextView, defaultTextColor: Int) {
            fun getAttrColor(@AttrRes attrId: Int): Int {
                val typedValue = TypedValue()
                context.theme.resolveAttribute(attrId, typedValue, true)
                return typedValue.data
            }
            val now = ZonedDateTime.now()
            // 间隔分钟数
            val minutes = ChronoUnit.MINUTES.between(target, now)
            val hours = ChronoUnit.HOURS.between(target, now)
            val days = ChronoUnit.DAYS.between(target, now)
            val weeks = ChronoUnit.WEEKS.between(target, now)
            when {
                minutes < 5 -> {
                    textView.text = context.getString(R.string.time_just_now)
                    textView.setTextColor(context.getColor(R.color.green))
                }

                hours < 2 -> {
                    textView.text = context.getString(R.string.time_minutes_before, minutes.toString())
                    textView.setTextColor(getAttrColor(R.attr.textColorBlue))
                }

                days < 2 -> {
                    textView.text = context.getString(R.string.time_hours_before, hours.toString())
                    textView.setTextColor(defaultTextColor)
                }

                weeks < 2 -> {
                    textView.text = context.getString(R.string.time_days_before, days.toString())
                    textView.setTextColor(defaultTextColor)
                }

                target.year == now.year -> {
                    textView.text = DATE_TIME_FORMATTER_MONTH_MINUTE.format(target)
                    textView.setTextColor(getAttrColor(R.attr.textColorRed))
                }

                else -> {
                    textView.text = SIMPLE_DATE_TIME_FORMATTER.format(target)
                    textView.setTextColor(getAttrColor(R.attr.textColorRed))
                }
            }
        }
    }
}